<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class VideoService extends BaseService
{
    /**
     * 视频
     */

    /**
     * 视频列表
     * @param int $scenceId 场景ID
     * @param array $pages【start起始页，limit每页记录数】
     * return array
     */
    public function getIndex($scenceId, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbVideo . ' as t1')
            ->select('t1.*', 't2.name as scenceName', 't2.view as viewName', 't2.userType')
            ->leftJoin($this->dbScene . ' as t2', 't2.id', '=', 't1.scence_id');
        if ($scenceId) {
            $db = $db->where('t1.scence_id', $scenceId);
        }
        $total = $db->where('t1.del', 1)->count();
        $lists = $db->where('t1.del', 1)
            ->skip($start)->take($pages['limit'])
            ->get();
        if (empty($lists)) return [];
        $arys = [];
        foreach ($lists as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'cover' => getenv('API_DOMAIN').$v['cover'],
                'link' => getenv('API_DOMAIN').$v['link'],
                'dylink' => $v['dylink'],
                'playNum' => $v['playNum'],
                'scence_id' => $v['scence_id'],
                'scenceName' => $v['scenceName'],
                'useNum' => $v['useNum'],
                'userType' => $v['userType'],
                'viewName' => $v['viewName'],
                'addtime' => $v['addtime'],
                'uptime' => $v['uptime'],
            ];
        }
        if (!count($arys)) {
            return $this->toback(-2, '没有了');
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 新增
     * @param int $scence 场景
     * @param string $title 标题
     * @param string $cover 封面
     * @param string $link 视频地址
     * return array
     */
    public function setAdd($scene, $title, $cover, $link)
    {
        $scenceOne = (array) $this->dbConn($this->dbScene)
            ->where('del', 1)
            ->where('name', $scene)
            ->first();
        if (empty($scenceOne)) {
            return array('code' => -2, 'msg' => '场景没有');
        }
        $addData = [
            'scence_id' => $scenceOne['id'],
            'title' => $title,
            'cover' => $cover,
            'link' => $link,
            'addtime' => toDate(),
            'uptime' => toDate(),
        ];
        DB::beginTransaction(); //开启事务
        try {
            //视频
            $rst = $this->dbConn($this->dbVideo)->insertGetId($addData);
            if (!$rst) {
                return array('code' => -2, 'msg' => 'add fail');
            }
            //场景
            $rst = $this->dbConn($this->dbScene)
                ->where('id', $scenceOne['id'])
                ->increment('videoNum', 1);
            if (!$rst) {
                return array('code' => -2, 'msg' => 'scence fail');
            }
            DB::commit(); //提交事务
            return array('code' => 0, 'msg' => 'success', 'data' => $rst);
        } catch (\Exception $e) {
            DB::rollBack(); //事务回滚
            echo $e->getMessage();
            return array('code' => -1, 'msg' => 'error');
        }
    }

    /**
     * 修改
     * @param int $vid 视频ID
     * @param string $scence 场景
     * @param string $title 标题
     * @param string $cover 封面
     * @param string $link 视频地址
     * return array
     */
    public function setModify($vid, $scence, $title, $cover, $link)
    {
        //场景有没有
        $scenceOne = (array) $this->dbConn($this->dbScene)
            ->where('del', 1)
            ->where('name', $scence)
            ->first();
        if (empty($scenceOne)) {
            return array('code' => -2, 'msg' => '场景没有');
        }
        //视频有没有
        $res = (array) $this->dbConn($this->dbVideo)
            ->where('del', 1)
            ->where('id', $vid)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '视频不存在');
        }
        $scenceData = [
            'scence_id' => $scenceOne['id'],
            'title' => $title,
            'cover' => $cover,
            'link' => $link,
            'uptime' => toDate(),
        ];
        $rst = $this->dbConn($this->dbVideo)
            ->where('del', 1)
            ->where('id', $vid)
            ->update($scenceData);
        if (!$rst) {
            return array('code' => -2, 'msg' => 'update fail');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $rst);
    }

    /**
     * 删除
     * @param int $vid 视频ID
     * return array
     */
    public function setDel($vid)
    {
        $videoOne = (array) $this->dbConn($this->dbVideo)
            ->where('del', 1)
            ->where('id', $vid)
            ->first();
        if (empty($videoOne)) {
            return array('code' => -2, 'msg' => '视频不存在');
        }
        DB::beginTransaction(); //开启事务
        try {
            //视频
            $resVideo = $this->dbConn($this->dbVideo)
                ->where('del', 1)
                ->where('id', $vid)
                ->update(['del' => 2, 'deltime' => toDate()]);
            if (!$resVideo) {
                return array('code' => -2, 'msg' => 'delete fail');
            }
            //场景
            $resScence = $this->dbConn($this->dbScene)
                ->where('id', $videoOne['scence_id'])
                ->decrement('videoNum', 1);
            if (!$resScence) {
                return array('code' => -2, 'msg' => 'scence fail');
            }
            DB::commit(); //提交事务
            return array('code' => 0, 'msg' => 'success');
        } catch (\Exception $e) {
            DB::rollBack(); //事务回滚
            echo $e->getMessage();
            return array('code' => -1, 'msg' => 'error');
        }
    }
}